﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Scheduler.Data_Access_Layer
{
    class Login
    {
        /**Class: Login
        * @author Daniel Chopson
        * @version 1.0
        * Course : ITEC 4860 Spring 2012
        * Written: March 22, 2012
        *
        * This class connects to the User table in the database via a TableAdapter.
        * Getters and setters are provided for users.
        *
        * Purpose: To provide the data access neccessary for logging in via the
        * Login module.
        */

        //The user's ID

        private string userID;

        //The user's password

        private string password;
        
        //The adapter for User table connectivity

        private SchedulerTableAdapters.UserTableAdapter userAdapter;

        //The data table for temporarily holding user data

        private Scheduler.UserDataTable userData;

        //A single row from the User table

        private Scheduler.UserRow userRow;

        /** Method: Login
          * Constructor for this class. Instantiates all class variables
          * @param userID the user's ID
          * @param password the user's password
          */

        public Login(string userID, string password)
        {
            this.userID = userID;
            this.password = password;
            userAdapter = new SchedulerTableAdapters.UserTableAdapter();
        }

        /** Method: doesUserExist
          * Checks to see if the user exists in the database. MUST BE CALLED BEFORE ALL OTHER 
          * METHODS, otherwise userRow will not be set. userRow will only be set if the user 
          * exists. None of the other methods will work unless the userRow is set.
          * @return a boolean to denote whether the user was found
          */

        public bool doesUserExist()
        {
            userData = userAdapter.GetDataByUserID(userID);
            if (userData.Count == 0)
            {
                return false;
            }
            else
            {
                userRow = userData[0];
            }
            return true;
        }

        /** Method: isPasswordValid
          * Checks to see if the user's password is valid
          * @return a boolean to denote whether the password was valid
          */

        public bool isPasswordValid()
        {
            if (userRow.Password.Trim() == password)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /** Method: getIsAcctLocked
          * Gets the user's IsAcctLocked status
          * @return IsAcctLocked from the User table
          */

        public bool getIsAcctLocked()
        {
            return userRow.IsAcctLocked;
        }

        /** Method: setIsAcctLocked
          * Sets the user's IsAcctLocked status
          * @param isAcctLocked the user's IsAcctLocked status
          */

        public void setIsAcctLocked(bool isAcctLocked)
        {
            userRow.IsAcctLocked = isAcctLocked;
            userAdapter.Update(userData);
        }

        /** Method: getIsAcctActive
          * Gets the user's IsAcctActive status
          * @return IsAcctActive from the User table
          */

        public bool getIsAcctActive()
        {
            return userRow.IsAcctActive;
        }

        /** Method: setIsAcctActive
          * Sets the user's IsAcctActive status
          * @param isAcctActive the user's IsAcctActive status
          */

        public void setIsAcctActive(bool isAcctActive)
        {
            userRow.IsAcctActive = isAcctActive;
            userAdapter.Update(userData);
        }

        /** Method: isEmployee
          * Checks to see if the userID exists in the Employee table
          * @return a boolean to denote whether the use was found in the Employee table
          */

        public bool isEmployee()
        {
            SchedulerTableAdapters.EmployeeTableAdapter employeeAdapter = new SchedulerTableAdapters.EmployeeTableAdapter();
            Scheduler.EmployeeDataTable employeeData = employeeAdapter.GetData();
            foreach (Scheduler.EmployeeRow employeeRow in employeeData)
            {
                if (employeeRow.EmpUserID.Trim() == userID)
                {
                    return true;
                }
            }
            return false;
        }

        /** Method: isManager
          * Checks to see if the userID exists in the Manager table
          * @return a boolean to denote whether the use was found in the Manager table
          */

        public bool isManager()
        {
            SchedulerTableAdapters.ManagerTableAdapter managerAdapter = new SchedulerTableAdapters.ManagerTableAdapter();
            Scheduler.ManagerDataTable managerData = managerAdapter.GetData();
            foreach (Scheduler.ManagerRow managerRow in managerData)
            {
                if (managerRow.MgrUserID.Trim() == userID)
                {
                    return true;
                }
            }
            return false;
        }
    }
}